package monster.com.cvh.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.bean.PostBean;
import monster.com.cvh.fragment.base.PermissionFragment;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.ResgisterButton;
import okhttp3.Call;
import okhttp3.Response;


/**
 * @author: 13580
 * @data: 2018/3/7
 * @function:
 */

public class RegisterFragment extends PermissionFragment {
    @BindView(R.id.iv_nomal_back)
    ImageView mIvNomalBack;
    @BindView(R.id.iv_phone)
    ImageView mIvPhone;
    @BindView(R.id.edt_activity_register_phone)
    EditText mEdtActivityRegisterPhone;
    @BindView(R.id.iv_check)
    ImageView mIvCheck;
    @BindView(R.id.tv_activity_register_code)
    TextView mTvActivityRegisterCode;
    @BindView(R.id.iv_name)
    ImageView mIvName;
    @BindView(R.id.edt_activity_register_user_name)
    EditText mEdtActivityRegisterUserName;
    @BindView(R.id.iv_password)
    ImageView mIvPassword;
    @BindView(R.id.edt_activity_register_password)
    EditText mEdtActivityRegisterPassword;
    @BindView(R.id.tv_activity_register_commit)
    TextView mTvActivityRegisterCommit;
    @BindView(R.id.iv_activity_register_nosee)
    ImageView mIvActivityRegisterNosee;
    @BindView(R.id.edt_activity_register_sms_code)
    EditText mEdtActivityRegisterSmsCode;
    private Boolean showPassword = true;
    private Gson gson;
    protected ProgressDialog mDialog;//网络请求ProgressDialog

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialog = new ProgressDialog(getContext());
        mDialog.setMessage(getResources().getString(R.string.wait));
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_register;
    }

    @Override
    protected void initData() {
        gson = new Gson();
    }

    @Override
    protected void initEvent() {

    }

    @OnClick(R.id.iv_nomal_back)
    public void onViewClicked() {
        getFragmentManager().popBackStack();
    }


    /**
     * 获取短信验证码
     */
    @OnClick(R.id.tv_activity_register_code)
    public void onMTvActivityRegisterCodeClicked() {
        ResgisterButton button = new ResgisterButton(mTvActivityRegisterCode, 60000, 1000);
        button.start();
        OkGo.<String>post(MyConstant.SMS_CODE_URL).params("phone", mEdtActivityRegisterPhone.getText().toString().trim()).execute(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    PostBean postBean = gson.fromJson(s, PostBean.class);
                    switch (postBean.getCode()) {
                        case 1:
                            //请求成功
                            showToastLong("获取验证码成功");
                            break;
                        case -1004:
                            showToastLong("请输入手机号码");
                            break;
                        case 10:
                            showToastLong("手机号码填写错误");
                            break;
                    }
                } catch (Exception e) {
                    showToastLong("获取信息失败，稍后重试");
                    return;
                }
            }
        });
    }

    /**
     * 注册按钮
     */
    @OnClick(R.id.tv_activity_register_commit)
    public void onMTvActivityRegisterCommitClicked() {
        mDialog.show();
        OkGo.<String>post(MyConstant.PHONE_REGISTER)
                .params("phone", mEdtActivityRegisterPhone.getText().toString().trim())
                .params("pwd", mEdtActivityRegisterPassword.getText().toString().trim())
                .params("name", mEdtActivityRegisterUserName.getText().toString().trim())
                .params("smsCode", mEdtActivityRegisterSmsCode.getText().toString().trim())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            PostBean postBean = gson.fromJson(s, PostBean.class);
                            switch (postBean.getCode()) {
                                case 1:
                                    showToastLong("注册成功");
                                    getFragmentManager().popBackStack();
                                    break;
                                case 10:
                                    showToastLong("无效的手机号");
                                    break;
                                case 16:
                                    showToastLong("验证码不能为空");
                                    break;
                                case 19:
                                    showToastLong("验证码输入错误");
                                    break;
                                case 20:
                                    showToastLong("该手机已注册");
                                    break;
                                case 22:
                                    showToastLong("昵称已存在");
                                    break;
                                case -1004:
                                    showToastLong("请将信息填写完整");
                                    break;
                                case -1005:
                                    showToastLong("验证码已失效");
                                    break;

                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            showToastLong(R.string.net_load_failed);
                        } finally {
                            mDialog.dismiss();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        mDialog.dismiss();
                        showToastLong(R.string.net_load_failed);
                    }
                });
    }

    /**
     * 显示隐藏密码
     */
    @OnClick(R.id.iv_activity_register_nosee)
    public void mIvActivityRegisterNoseeClicked() {
        //显示密码
        if (showPassword) {
            mIvActivityRegisterNosee.setImageResource(R.mipmap.login_see);
            mEdtActivityRegisterPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            mEdtActivityRegisterPassword.setSelection(mEdtActivityRegisterPassword.getText().toString().length());
            showPassword = !showPassword;
        } else {
            //隐藏密码
            mEdtActivityRegisterPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            mEdtActivityRegisterPassword.setSelection(mEdtActivityRegisterPassword.getText().toString().length());
            mIvActivityRegisterNosee.setImageResource(R.mipmap.login_notsee);
            showPassword = !showPassword;
        }
    }

}
